<?php
namespace App\Controller;

/**
 * 我要上电视活动
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class TvController extends AppController
{
    public function _initialize() {
        parent::_initialize();

        $this->prefix = "#我要上电视#";
        $this->game_id = 8; // 守望先锋
        $this->start = strtotime('2016-07-21 00:00:00');
        $this->end = strtotime('2016-07-28 23:59:59');

        $this->_index = 0;
    }

    public function index() {
        $p = I('p', 1, 'intval');
        $p < 1 && $p = 1;
        $limit     = I('limit', IS_MOBILE ? 8 : 12, 'intval');

        $videos = $this->_video($this->prefix, $p, $limit);

        if (IS_AJAX) {
            $this->ajaxReturn(['state' => 0, 'data' => $videos]);
        }
        $this->assign('winningVideos', $this->_winningVideos());
        $this->assign('videos', $videos);
        $this->assign('limit', $limit);
        $this->assign('title', "我要上电视 OW【66Play】一键记录你的游戏青春");
        if (IS_MOBILE) {
            $token = I('appToken');
            if (!empty($token)) {
                $this->assign('isFromApp', true);
            }
            return $this->display('mobile');
        }
        $total = $this->_count($this->prefix);
        $this->assign('total', $total);
        $this->display();
    }

    private function _video($prefix, $p = 1, $limit = 12)
    {
        $service = D('Video', 'Service');

        $map['title'] = new \MongoRegex("/^$prefix/i");
        $map['status'] = $service::STATUS_PASS;
        $map['type'] = $service::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $map['game_id'] = $this->game_id;

        !empty($this->start) && $map['create_time']['$gte'] = $this->start;
        !empty($this->end) && $map['create_time']['$lte'] = $this->end;

        $res = $service->where($map)->field('id')
                       ->order('create_time desc')
                       ->limit(($p-1)*$limit, $limit)
                       ->select();

        $vEvent = event('video');
        $fieldMap = ['id', 'title', 'create_time', 'time', 'user', 'statis', 'uri', 'pre_uri', 'thumb'];
        $list = [];
        foreach ($res as $value) {
            $vid = $value['id'];
            $video = $vEvent->detail($vid);
            $video = $this->field_filter($video, $fieldMap);
            $list[] = $video;
        }
        return $list;
    }

    private function _count($prefix) {
        $service = D('Video', 'Service');

        $map['title'] = new \MongoRegex("/^$prefix/i");
        $map['status'] = $service::STATUS_PASS;
        $map['type'] = $service::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $map['game_id'] = $this->game_id;

        !empty($this->start) && $map['create_time']['$gte'] = $this->start;
        !empty($this->end) && $map['create_time']['$lte'] = $this->end;

        return $service->where($map)->count();
    }

    private function _winningVideos() {
        $ids = C('TV_WINNER');
        if (empty($ids)) {
            return [];
        }
        $list = explode('|', $ids);
        if (!is_array($list)) {
            return [];
        }
        $str = $list[$this->_index];
        $arr = explode(':', $str);
        $date = $arr[0];
        $ids = array_map('intval', explode(',', $arr[1]));
        $video_id = $ids[0];
        $uids = array_slice($ids, 1);
        $uEvent = event('user');
        $users = [];
        foreach ($uids as $value) {
            $user = $uEvent->info($value, false, 'id,nickname,avatar', false);
            $users[] = $user;
        }
        return compact('date', 'video_id', 'users');
    }
}